package ui;

import burp.*;
import result.AttackResult;
import result.ModelRoot;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.io.IOException;
import java.util.*;

/**
 * Created by td on 2017/7/1.
 */
public class MainUi  extends AbstractTableModel implements ITab, IMessageEditorController,Observer {

    private IMessageEditor requestViewer;

    private IMessageEditor responseViewer;

    private JSplitPane splitPane;

    private IHttpRequestResponse currentDisplayItem;

    private IBurpExtenderCallbacks callbacks;


    public MainUi(IBurpExtenderCallbacks callbacks) {
        this.callbacks = callbacks;
    }

    public void getUI(){
        ModelRoot.getModelRootInstance().addObserver(this);

        splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        splitPane.setContinuousLayout(true);
        Table table = new Table(MainUi.this);
        JScrollPane scrollPane = new JScrollPane(table);
        splitPane.setLeftComponent(scrollPane);
        splitPane.setContinuousLayout(true);

        JPanel jPanel1 = new JPanel();
        JTabbedPane tabs = new JTabbedPane();
        tabs.setLocation(10000,1000);

        requestViewer = callbacks.createMessageEditor(MainUi.this,false);
        responseViewer = callbacks.createMessageEditor(MainUi.this,false);


        tabs.addTab("Request",requestViewer.getComponent());
        tabs.addTab("Response",responseViewer.getComponent());
       // splitPane.setRightComponent(tabs);
        jPanel1.setPreferredSize(new Dimension(1000,150));
        jPanel1.add(tabs);


        splitPane.setRightComponent(jPanel1);





        this.callbacks.customizeUiComponent(splitPane);
        this.callbacks.customizeUiComponent(table);
        this.callbacks.customizeUiComponent(scrollPane);
        this.callbacks.customizeUiComponent(jPanel1);
     //   this.callbacks.customizeUiComponent(jPanel2);
        this.callbacks.customizeUiComponent(tabs);
        this.callbacks.addSuiteTab(MainUi.this);
        this.callbacks.createMessageEditor(this,true);



    }


    private class Table extends JTable{
        public Table(TableModel tableModel){
            super(tableModel);
        }

        @Override
        public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
             AttackResult attackResult = ModelRoot.getModelRootInstance().getAttackResultLinkedList().get(rowIndex);

            requestViewer.setMessage(attackResult.getBaseHttpResponse().getRequest(),true);
            responseViewer.setMessage(attackResult.getBaseHttpResponse().getResponse(),false);
            currentDisplayItem = attackResult.getBaseHttpResponse();

            super.changeSelection(rowIndex, columnIndex, toggle, extend);
        }




    }



    public String getTabCaption() {
        return "EMSqlCheck";
    }

    public Component getUiComponent() {
        return this.splitPane;
    }

    public IHttpService getHttpService() {
        return currentDisplayItem.getHttpService();
    }

    public byte[] getRequest() {
        return currentDisplayItem.getRequest();
    }

    public byte[] getResponse() {
        return currentDisplayItem.getResponse();
    }

    public int getRowCount() {
        return ModelRoot.getModelRootInstance().getAttackResultLinkedList().size();
    }

    public int getColumnCount() {
        return 5;
    }

    @Override
    public String getColumnName(int column) {
        switch (column){
            case 0:
                return "sqlInjcetType";
            case 1:
                return "URL";
            case 2:
                return "insertPointParam";
            case 3:
                return "PageNomalPayload";
            case 4:
                return "PageAbnomalPayload";
            default:
                return "";
        }

    }


    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }


    public Object getValueAt(int rowIndex, int columnIndex) {
        AttackResult attackResult =  ModelRoot.getModelRootInstance().getAttackResultLinkedList().get(rowIndex);
        switch (columnIndex){
            case 0:
                return attackResult.getSqlInjectType();
            case 1:
                return attackResult.getUrl();
            case 2:
                return attackResult.getInsertParam();
            case 3:
                return attackResult.getPageNomalPayload();
            case 4:
                return attackResult.getPageAbNomalPayload();
            default:
                return "";
        }

    }

    public void update(Observable o, Object arg) {
        AttackResult attackResult = (AttackResult) arg;

        int row = ModelRoot.getModelRootInstance().getAttackResultLinkedList().size();
        fireTableRowsInserted(row, row);

    }
}
